import json
import os.path
import openpyxl
from openpyxl.worksheet.worksheet import Worksheet
from common.config import *


# 操作excel文件的类
from common.config import excel_path


class DoExcel():
    # 读excel全部内容
    def readExcelS(self, fname):
        # 读excel文件
        excelFile1 = os.path.join(excel_path, fname)
        # 载入一个工作簿
        workbook = openpyxl.load_workbook(excelFile1)
        # 获取要处理的sheet页
        # 变量名：类型：是对变量的注解
        # 获取Sheet1的文件内容
        # sheet1: Worksheet = workbook.get_sheet_by_name("Sheet1")
        # 获取当前激活的sheet
        sheet1: Worksheet = workbook.active
        # #读sheet中的内容
        l = [i for i in sheet1.values]
        print("读取成功")
        print(l)
        return l

    # 读excel的部分内容
    def readExcelRowCol(self, fname, stname,minR=None, maxR=None, minC=None, maxC=None):
        # 读excel文件
        excelFile = os.path.join(excel_path, fname)
        # 载入一个工作簿
        workbook = openpyxl.load_workbook(excelFile)
        # 获取要处理的sheet页
        # 变量名：类型：是对变量的注解
        # sheet1: Worksheet = workbook.get_sheet_by_name("Sheet1")
        # 获取当前激活的sheet
        # sheet1: Worksheet = workbook.active
        sheet1: Worksheet = workbook[stname]
        # #读sheet1中的内容
        a = sheet1.iter_rows(min_row=minR, max_row=maxR, min_col=minC, max_col=maxC, values_only=True)
        l = [i for i in a]
        print("读取成功")
        print(l)
        return l

    # 写excel文件并且创建
    # 写文件
    def writeExcel(self, fname):
        # # 创建excel并写
        wk = openpyxl.Workbook()
        st = wk.create_sheet("Sheet1")
        st.cell(1, 1, "内容1")
        st.cell(1, 2, "内容2")
        excelFile = os.path.join(excel_path, fname)
        wk.save(excelFile)


class DoTxt():
    def readTxts(self, fname):
        txtfile = os.path.join(txt_path, fname)
        # 读txt或者log文件
        f = open(txtfile, mode='r', encoding='utf-8')
        # 读取全部内容
        strs = f.read()
        # 读取全部内容，返回列表
        # strs=f.readline()
        # 一行一行读取的
        # strs= f.readlines()
        print(strs)
        f.close()

    def writeTxts(self, fname):
        # 写   如果文件存在，则先清空，再写。如果文件不存在，则先创建再写!
        txtfile = os.path.join(txt_path, fname)
        f = open(txtfile, mode='w', encoding='utf-8')
        f.write("这是我写入的东西！！！！！！")
        f.close()
        print("写入成功")


class ChangeJson():
    def chang_Dict_json(self, fname):
        # 将字典转储成ison格式的文件
        d = {"name": "zhangsan", "age": 24}
        json1 = os.path.join(json_path, fname)
        with open(json1, mode='w')as f:
            json.dump(d, f)
        with open(json1, mode='r') as f:
            dd = json.load(f)
            print(dd, type(dd))
            return dd




class DoPicture():
    def readPicture(self,fname):
        # 读图片
        picturefile = os.path.join(picture_path, fname)
        f = open(picturefile, mode='rb')
        strs = f.read()#读取全部内容
        print(strs)
        f.close()
    def writePicture(self):
        import os.path
        # #写   如果文件存在，则先清空，再写。如果文件不存在，则先创建再写!
        picturefile = os.path.join(picture_path,"3.png")
        f = open(picturefile, mode='rb')
        strs = f.read()
        f= open(picturefile,mode='wb')
        f.write(strs)
        f.close()

        # # 忘了关闭咋办:with
        # import os.path
        # file = os.path.join(data_path, ".png")
        # with open(file,mode='rb')as f:
        #  strs = f.read()#读取全部内容
        # print(strs)
        # # #写   如果文件存在，则先清空，再写。如果文件不存在，则先创建再写!
        # file2 = os.path.join(data_path, ".png")
        # f= open(file2,mode='wb')
        # f.write(strs)
        # f.close()


# if __name__ == '__main__':
# # 测试读取全部内容
# d1 = DoExcel()
# d1.readExcelS(fname="1.xlsx")
# #   测试读取部分内容
# d2 = DoExcel()
# d2.readExcelRowCol(fname="1.xlsx",minR=1,maxR=4,minC=1,maxC=4)
#     测试写文件
# w1=DoExcel()
# w1.writeExcel(fname="2.xlsx")
#     测试读取文件内容
# t1=DoTxt()
# t1.readTxts(fname="1.txt")
# 测试写文件内容
# t2=DoTxt()
# t2.writeTxts(fname="2.txt")

# 测试字典转成json格式
# cj=ChangeJson()
# cj.chang_Dict_json(fname="ljk.json")
# 测试看图片
# rp1=DoPicture()
# rp1.readPicture(fname="3.png")